package com.ph.website.home.base.utils;

import java.util.*;

public class StringUtil {
    /**
     * 用","分割的字符串转换成List<String>
     */
    public static List<String> string2List(String str){
        List<String> list = new ArrayList<>();
        if(str!=null && str.trim().length()>0){
            String[] strs = str.split(",");
            for(String s : strs){
                list.add(s);
            }
        }
        return list;
    }

    /**
     * List<String>转换成用","分割的字符串
     */
    public static String list2String(List<String> list){
        String result = "";
        if(list!=null && list.size()>0){
            StringBuilder stringBuilder = new StringBuilder();
            for(String str : list){
                stringBuilder.append(",");
                stringBuilder.append(str);
            }
            result = stringBuilder.substring(1);
        }
        return result;
    }

    /**
     * List<Integer>转换成","分割的字符串
     */
    public static String listInt2String(List<Integer> list){
        String result = "";

        if(list!=null && list.size()>0){
            StringBuilder stringBuilder = new StringBuilder();
            for(Integer i : list){
                stringBuilder.append(",");
                stringBuilder.append(String.valueOf(i));
            }
            result = stringBuilder.substring(1);
        }
        return result;
    }

    /**
     * 用","分割的字符串转换成List<String>
     */
    public static List<Integer> string2ListInt(String str){
        List<Integer> list = new ArrayList<>();
        if(str!=null && str.trim().length()>0){
            String[] strings = str.split(",");
            for(String s : strings){
                list.add(Integer.parseInt(s));
            }
        }
        return list;
    }

    /**
     * 判断字符串是否为空
     */
    public static boolean isEmpty(String str){
        if(str==null){
            return true;
        }
        if(str.trim().length()==0){
            return true;
        }
        return false;
    }

    /**
     * 获取毫秒数+6位随机数字
     */
    public static String randomName(){
        long time = new Date().getTime();
        String result = String.valueOf(time);

        Random random = new Random();
        for(int i=0;i<6;i++){
            int randomInt = random.nextInt(10);
            result = result + String.valueOf(randomInt);
        }
        return result;
    }

    /**
     * 获取6位随机数字
     */
    public static String random6(){

        String result = "";
        Random random = new Random();
        for(int i=0;i<6;i++){
            int randomInt = random.nextInt(10);
            result = result + String.valueOf(randomInt);
        }
        return result;
    }

    /**
     * 获取uuid
     */
    public static String getUUid(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-","");
    }

    /**
     * 获取文件后缀名
     */
    public static String getSuffix(String fileName){
        if(StringUtil.isEmpty(fileName)){
            return null;
        }
        int index = fileName.indexOf(".");
        if(index<0){
            return null;
        }
        return fileName.substring(index);
    }

    /**
     * 判断是否是IE Edge 等微软浏览器
     */
    public static boolean isMSBrowser(String userAgent){
        String[] IEBrowserSignals = {"MSIE", "Trident", "Edge"};
        for (String signal : IEBrowserSignals) {
            if (userAgent.contains(signal))
                return true;
        }
        return false;
    }

    /**
     * 将二进制转换成16进制字符串
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
    /**
     * 将16进制字符串转换为二进制
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

}
